package com.fastCms.business.frame.busi.impl;

import com.fastCms.business.frame.constant.ResultCodeConst;
import com.fastCms.business.frame.enums.AccountStatusEnum;
import com.fastCms.business.frame.enums.PlatformEnum;
import com.fastCms.business.frame.busi.CloudOrganizeManageService;
import com.fastCms.business.frame.pojo.PageParam;
import com.fastCms.business.frame.pojo.entity.CloudOrganize;
import com.fastCms.business.frame.pojo.entity.CloudUser;
import com.fastCms.business.frame.pojo.po.EditOrganizeStatusPO;
import com.fastCms.business.frame.pojo.po.QueryOrganizePO;
import com.fastCms.business.frame.pojo.vo.CloudOrganizePO;
import com.fastCms.business.frame.response.ApiResponse;
import com.fastCms.business.frame.response.ResponseUtil;
import com.fastCms.business.frame.service.CloudOrganizeService;
import com.fastCms.business.frame.service.CloudUserService;
import com.fastCms.business.frame.utils.CommonsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 哈密瓜科技 版权所有 © Copyright 2019<br>
 *
 * @Description: 登录权限控制<br>
 * @Project: hami <br>
 * @CreateDate: Created in 2019/3/7 下午3:48 <br>
 * @Author: <a href="lsquan666@163.com">lsquan</a>
 */
@Component
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class CloudOrganizeManageServiceImpl implements CloudOrganizeManageService {

    @Autowired
    private CloudOrganizeService cloudOrganizeServiceImpl;

    @Autowired
    private CloudUserService cloudUserServiceImpl;

    @Override
    public ApiResponse<PageInfo<CloudOrganize>> showOrganizes(PageParam<QueryOrganizePO> queryOrganizePO) {
        PageInfo<CloudOrganize> cloudOrganizePageInfo = PageHelper.startPage(queryOrganizePO.getPageNum(), queryOrganizePO.getPageSize())
            .doSelectPageInfo(() ->
                cloudOrganizeServiceImpl.findByQueryOrganizePO(queryOrganizePO));
        return ResponseUtil.success(cloudOrganizePageInfo);
    }

    @Override
    public ApiResponse<CloudOrganize> editOrganize(CloudOrganizePO cloudOrganizePO) {
        if (StringUtils.isBlank(cloudOrganizePO.getOrganizeId())) {
            return ResponseUtil.fail(ResultCodeConst.PARAM_ERROR, ResultCodeConst.PARAM_ERROR_MSG);
        }
        CloudOrganize cloudOrganize = cloudOrganizeServiceImpl.findById(cloudOrganizePO.getOrganizeId());
        cloudOrganize.setIntroduction(cloudOrganizePO.getIntroduction());
        cloudOrganize.setName(cloudOrganizePO.getName());
        cloudOrganize.setOrganizeName(cloudOrganizePO.getOrganizeName());
        boolean a = cloudOrganizeServiceImpl.saveOrUpdate(cloudOrganize);

        //修改组织下的管理员姓名
        CloudUser cloudUser = cloudUserServiceImpl.findByPhone(cloudOrganize.getPhone(), cloudOrganizePO.getPlatform());
        if (cloudUser == null) {
            return ResponseUtil.fail(ResultCodeConst.DATA_ERROR, ResultCodeConst.DATA_ERROR_MSG);
        }
        cloudUser.setName(cloudOrganizePO.getName());
        boolean b = cloudUserServiceImpl.saveOrUpdate(cloudUser);
        if (a && b) {
            return ResponseUtil.success(cloudOrganize);
        }
        return ResponseUtil.fail();

    }

    @Override
    public ApiResponse editOrganizeStatus(EditOrganizeStatusPO statusPO) {

        //1.数据存在性
        CloudOrganize organize = cloudOrganizeServiceImpl.findByOrganizeCode(statusPO.getOrganizeCode());
        if (organize == null) {
            return ResponseUtil.fail(ResultCodeConst.DATA_ERROR, ResultCodeConst.DATA_ERROR_MSG);
        }
        //禁用组织下所有的帐号
        Boolean result = cloudUserServiceImpl.updateUserStatus(statusPO.getOrganizeCode(), statusPO.getStatus());
        if (result) {
            organize.setStatus(statusPO.getStatus());
            cloudOrganizeServiceImpl.saveOrUpdate(organize);
            return ResponseUtil.success(true);
        }
        return ResponseUtil.fail(ResultCodeConst.DATA_ERROR, ResultCodeConst.DATA_ERROR_MSG);
    }


    @Override
    public ApiResponse createOrganize(CloudOrganizePO organizePO) {

        String phone = organizePO.getPhone();
        CloudOrganize organize = cloudOrganizeServiceImpl.findByPhone(phone);
        if (organize != null) {
            return ResponseUtil.fail(ResultCodeConst.USER.PHONE_BE_REGISTER, ResultCodeConst.USER.PHONE_BE_REGISTER_MGS);
        }
        CloudOrganize organizeNameBean = cloudOrganizeServiceImpl.findByOrganizeName(organizePO.getOrganizeName());
        if (organizeNameBean != null) {
            return ResponseUtil.fail(ResultCodeConst.ORGANIZE.ORGANIZE_NAME_EXISTS, ResultCodeConst.ORGANIZE.ORGANIZE_NAME_EXISTS_MSG);
        }
        Date now = new Date();

        //1.录入组织信息
        CloudOrganize cloudOrganize = new CloudOrganize();
        CommonsUtil.initEntity(cloudOrganize);
        cloudOrganize.setOrganizeName(organizePO.getOrganizeName());
        cloudOrganize.setOrganizeCode(CommonsUtil.generateEnglishChar(6) + CommonsUtil.getRandomNum(6));
        cloudOrganize.setPaOrganizeCode("SUPER");  //临时放置
        cloudOrganize.setName(organizePO.getName());
        cloudOrganize.setPhone(organizePO.getPhone());
        cloudOrganize.setApplyTime(now);
        cloudOrganize.setIntroduction(organizePO.getIntroduction());
        cloudOrganize.setStatus(AccountStatusEnum.CAN_USE.getCode());
        boolean organizeResult = cloudOrganizeServiceImpl.saveOrUpdate(cloudOrganize);

        //3.创建组织管理员
        CloudUser cloudUser = new CloudUser();
        CommonsUtil.initEntity(cloudUser);
        cloudUser.setAdminType(1); //管理员
        cloudUser.setName(organizePO.getName());
        cloudUser.setPhone(organizePO.getPhone());
        cloudUser.setPlatform(organizePO.getPlatform());
        cloudUser.setOrganizeCode(cloudOrganize.getOrganizeCode());

        boolean userResult = cloudUserServiceImpl.saveOrUpdate(cloudUser);

        if (organizeResult && userResult) {
            return ResponseUtil.success(true);
        }
        return ResponseUtil.fail("创建失败");
    }
}
